package cn.zwx.order.query.svc.rpc;

import cn.zwx.base.entity.ResponseEntity;
import cn.zwx.base.exception.BizWrapException;
import cn.zwx.order.query.svc.domain.entity.OrderEntity;
import cn.zwx.order.query.svc.domain.enums.OrderQueryCodeEnum;
import cn.zwx.order.query.svc.rpc.dto.OrderInfoDTO;
import cn.zwx.order.query.svc.rpc.facade.DubboOrderQueryService;
import cn.zwx.order.query.svc.rpc.vo.OrderInfoVO;
import cn.zwx.order.query.svc.service.OrderService;
import com.alibaba.fastjson.JSON;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import org.apache.dubbo.apidocs.annotations.ApiDoc;
import org.apache.dubbo.apidocs.annotations.ApiModule;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author zhangwenxue
 */
@Service
@ApiModule(value = "查询订单信息相关接口", apiInterface = DubboOrderQueryService.class)
@DubboService(version = "v1.0.0",interfaceClass = DubboOrderQueryService.class,timeout = 8000)
public class DubboOrderQueryServiceImpl implements DubboOrderQueryService {

    public static final Logger logger = LoggerFactory.getLogger(DubboOrderQueryServiceImpl.class);
    @Resource
    private OrderService orderService;


    @Override
    @ApiDoc(value = "根据订单id查询用户信息接口")
    @RateLimiter(name="limiterA")
    public ResponseEntity<OrderInfoVO> queryOrderInfo(OrderInfoDTO dto) throws BizWrapException {
       // 1.查询订单信息
       OrderEntity orderEntity =  orderService.selectByOrderId(dto.getOrderId());
       if (Objects.isNull(orderEntity)){
           logger.error("未查询到订单信息,请求参数:{}", JSON.toJSONString(dto));
         throw new BizWrapException(OrderQueryCodeEnum.ORDER_NOT_EXIST_ERROR.getCode(),OrderQueryCodeEnum.ORDER_NOT_EXIST_ERROR.getMsg());
       }
       // 2.返回参数封装
       OrderInfoVO orderInfoVO = new OrderInfoVO();
       orderInfoVO.setAmount(orderEntity.getOrderAmount());
       orderInfoVO.setOrderId(orderEntity.getOrderId());
       orderInfoVO.setStatus(orderEntity.getOrderStatus());
       orderInfoVO.setDesc(orderEntity.getOrderDescription());
       return ResponseEntity.success(orderInfoVO);
    }
}
